<template>
	<BaseContainer
		class="report-table-wrapper"
		v-loading="loading"
		element-loading-text="加载中..."
		element-loading-background="rgba(255, 255, 255, 0.8)"
	>
		<div class="t-header">
			<div class="article-title">
				<span>南京市养老机构新增床位实地勘察报告</span>
			</div>
		</div>
		<ReportTable :column="dataList" :isDetail="isDetail" :moreNewsModel="moreNewsModel" :baseModel="baseModel" :model="modelData" @change="handleSave">
			<template v-slot:noteS>
				<div class="w-100 tr flex-column" style="line-height:30px">
          <p class="fc">经实地查看，以上数据真实有效。</p>
					<div class="p-r-50">
						<p>街（镇）民政部门：（章）</p>
						<p style="padding-right: 3vw">年 月 日</p>
					</div>
				</div>
			</template>
			<template v-slot:noteT>
				<div class="w-100 tr flex-column" style="line-height: 30px">
          <p class="fc">经实地查看，以上数据真实有效。</p>
					<div class="p-r-50">
						<p> 第三方机构：（章）</p>
						<p style="padding-right: 3vw">年 月 日</p>
					</div>
				</div>
			</template>
			<template v-slot:note>
				<div class="w-100 tr flex-column" style="line-height: 30px">
					<div class="p-r-50">
						<p>区民政部门勘察人签名:</p>
						<p style="padding-right: 3vw">年 月 日</p>
					</div>
				</div>
			</template>
		</ReportTable>
		<OrgArchives ref="orgArchivesEl"></OrgArchives>
	</BaseContainer>
</template>

<script setup lang="ts">
import ReportTable from '../ReportTable/index.vue'
import OrgArchives from '../OrgArchives/index.vue'
import apiMod from '@/apiMod'

const props = defineProps<{
	otherData?: any
	params?: any
	creditCode?: string | number
	editData?: any
	isDetail?: boolean
	caseId?: string | number
	custome?: boolean
	approvalStatus?: string | number
	applyNo?: string | number
}>()
const emit = defineEmits(['save'])

const model = ref({
	year: '',
	month: '',
})
const baseModel: any = shallowRef({})
const loading = ref(false)
const modelData: any = ref({})
const orgArchivesEl = ref(null)
let moreNewsModel: any = ref({})

const tableColumnConfig = (data?: any) => {
	if (!data) data = []
	const dataColumn = data.map((item) => {
		if (item.isMultiple == 1) {
			return [{ m: item.object }, { m: item.notes }, { m: item.area }, { m: item.facilities }]
		} else {
			return [
				{ m: item.object },
				{
					m: item.notes,
					mc: {
						rs: 1,
						cs: 3,
					},
				},
			]
		}
	})
	return [
		[
			{
				m: '被勘察机构概况',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{ m: '机构名称', style: { width: '200px' } },
			{
				m: '',
				prop: 'orgName',
				linkType: true,
				setColor: true,
				showLink: (col, model) => {
					orgArchivesEl.value?.getServiceOrgInfo(model.creditCode)
				},
			},
			{ m: '机构地址', style: { width: '200px' } },
			{
				m: '',
				setColor: true,
				prop: 'orgAddress',
				mc: {
					rs: 1,
					cs: 3,
				},
			},
		],
		[
			{ m: '联系人' },
			{
				m: '',
				prop: 'burdenUserName',
				setColor: true,
			},
			{ m: '电话' },
			{ m: '', prop: 'burdenUserPhone', setColor: true },
		],
		[
			{
				m: '勘察项目与数据',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{
				m: '项目',
				bl: 1,
				fs: 16,
			},
			{
				m: '地 址',
				bl: 1,
				fs: 16,
			},
			{
				m: '面 积',
				smallM: '（平方米）',
				bl: 1,
				fs: 16,
			},
			{
				m: '主要设施',
				bl: 1,
				fs: 16,
			},
		],
		...dataColumn,
		[
			{
				m: '备注',
			},
			{
				m: '',
				prop: 'noteText',
				mc: {
					rs: 1,
					cs: 3,
				},
			},
		],
		[
			{
				m: '勘察人意见',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		 [
			{
				m: '街（镇）民政部门意见',
				align: 'left',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
      {
				prop: 'noteS',
				align: 'left',
				mc: {
					rs: 1,
					cs: 3,
				},
			},
		],
    [
			{
				m: '第三方现场核查',
				align: 'left',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
      {
				prop: 'noteT',
				align: 'left',
				mc: {
					rs: 1,
					cs: 3,
				},
			},
		],
    [
			{
				m: '区民政部门意见',
				align: 'left',
				mc: {
					rs: 1,
					cs:1,
				},
			},
      {
				prop: 'note',
				align: 'left',
				mc: {
					rs: 1,
					cs: 3,
				},
			},
		],
	]
}
const dataList = ref(tableColumnConfig())

const getInitData = async () => {
	const params = props.params || {}
	const query = {
		applyNo: props.applyNo || props.editData?.formId,
		year: '',
		month: '',
		creditCode: props.creditCode,
		projectId: params.subsidyProjectId,
		applySource: !props.approvalStatus ? '-99' : props.approvalStatus,
	}
	if (params.belongDate) {
		query.year = params.belongDate.replace('年', '-').replace('月', '').split('-')[0]
		query.month = params.belongDate.replace('年', '-').replace('月', '').split('-')[1]
		model.value.year = query.year
		model.value.month = query.month
	} else {
		return
	}

	try {
		loading.value = true
		const condition = (props.caseId && props.isDetail) || props.custome || props.approvalStatus == -2
    modelData.value = {
			...props.editData,
		}
    if(!condition && !props.otherData?.allData) {
		  const uptoData = (await apiMod.view_ofsSubsidyApplySurvey_list(query).then((res) => res.data)) || []
      modelData.value.list = uptoData.map((item,index)=> {
        if(index ==0) {
          modelData.value.orgName=item.orgName
          modelData.value.orgAddress=item.orgAddress
          modelData.value.burdenUserName=item.burdenUserName
          modelData.value.burdenUserPhone=item.burdenUserPhone
        }
        return  {
          applyNo:item.applyNo,
          isMultiple:item.isMultiple,
          notes:item.notes,
          object:item.object,
          area:item.area,
          facilities:item.facilities,
        }
      })
    }

		loading.value = false
	
		baseModel.value = {
			orgName: modelData.value.orgName,
      orgAddress: modelData.value.orgAddress,
      burdenUserName: modelData.value.burdenUserName,
      burdenUserPhone: modelData.value.burdenUserPhone,
		}
		dataList.value = tableColumnConfig(modelData.value.list)
		if (!condition) handleSave()
	} catch (error) {
		loading.value = false
	}
}
onMounted(() => {
	if (props.params) getInitData()
})

const handleSave = () => {
	emit('save', modelData.value, 'bed_report_1010', props.editData)
}

onUnmounted(() => {})
</script>

<style scoped lang="scss">
.report-table-wrapper {
	width: 100%;
	margin: 0 auto;
	padding: 10px 30px;
	top: 0;

	.t-header {
		text-align: center;
		font-size: 18px;
		font-weight: bold;
		padding: 15px 0;

		.text-sm {
			font-weight: 400;
			font-size: 16px;
		}
	}
}

.article-title {
	position: relative;
	z-index: 0;
	width: 63%;
	margin: 0 auto;
}

.text-tip {
	--base-text-bg: rgb(56 176 255 / 13%);
	position: absolute;
	width: 100%;
	height: 100%;
	background: var(--base-text-bg);
	padding: 4px;
	z-index: -1;

	&:hover {
		cursor: pointer;
	}

	.tip-text-new {
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
		background-color: #ed6d05;
	}

	.tip-text-detail {
		background-color: #179fe3;
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
	}
}
</style>
